import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from "../views/Index"
import Register from "../views/Register"
import NotFound from "../views/404"
import Login from "../views/Login"
import Home from '../views/Home.vue'
import InfoShow from '../views/InfoShow.vue'
import FundList from '../views/FundList.vue'

Vue.use(VueRouter)


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes:[
      {
        path: '/',
        redirect: '/index',
      },
      {
        path: '/logout',
        redirect: '/login',
      },
      {
        path: '/index',
        name:"index",
        component:Index,
        children:[
          {path:'',name:"home",component:Home},
          {path:'/home',name:"home",component:Home},
          {path:'/infoshow',name:"infoshow",component:InfoShow},
          {path:'/fundlist',name:"fundlist",component:FundList},
        ]
      },
      {
        path: '/login',
        name:"login",
        component:Login
      },
      {
        path: '/register',
        name:"register",
        component:Register
      },
      {
        path: '*',
        name:"NotFound",
        component:NotFound
      },
  ]
})
//实现路由守卫
router.beforeEach((to,from,next) => {
  const isLogin = localStorage.userToken ? true : false;
  if (to.path == "/login" || to.path == "/register") {
    next();
  } else {
    isLogin ? next() : next("/login");
  }
})

export default router
